package edu.stthomas.edu.seis770.simpleclock;

import java.util.Observable;

class ClockModel extends Observable {
    private int seconds;
    private int minutes;
    private int hours;

    private ClockState currentState;

    public ClockModel(int seconds, int minutes, int hours) {
        this.seconds = seconds;
        this.minutes = minutes;
        this.hours = hours;
        // set current state to default
        currentState = ClockStateFactory.getClockState(DisplayTime.class, this);
    }

    public int getSeconds() {
        return seconds;
    }

    public int getMinutes() {
        return minutes;
    }

    public int getHours() {
        return hours;
    }


    public void setSeconds(int seconds) {
        if (seconds >= 59) {
            this.seconds = 60 - seconds;
            this.setMinutes(getMinutes() + 1);
        } else if (seconds < 0) {
            this.seconds = 60 + seconds;
            this.setMinutes(getMinutes() - 1);
        } else {
            this.seconds = seconds;
        }
        setChanged();
        notifyObservers();
    }

    public void setMinutes(int minutes) {
        if (minutes >= 59) {
            this.minutes = 60 - minutes;
            this.setHours(getHours() + 1);
        } else if (minutes < 0) {
            this.minutes = 60 + minutes;
            this.setHours(getHours() - 1);
        } else {
            this.minutes = minutes;
        }
        setChanged();
        notifyObservers();
    }

    public void setHours(int hours) {
        if (hours > 12) {
            this.hours = 1;
        } else if (hours <= 0) {
            this.hours = 12;
        } else {
            this.hours = hours;
        }
        setChanged();
        notifyObservers();
    }

    public void setCurrentState(ClockState clockState) {
        this.currentState = clockState;
        setChanged();
        // Pass current state to help ClockUI decide what buttons to display
        notifyObservers(currentState);
    }

    public void timerTick() {
        currentState.timerTick();
    }

    public void changeMode() {
        currentState.changeMode();
    }

    public void increment() {
        currentState.increment();
    }

    public void decrement() {
        currentState.decrement();
    }

    public void cancel() {
        currentState.cancel();
    }


}
